package com.icxcc.icxwebflux.handler;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;

@Component
public class TestHandler {
    public Mono<ServerResponse> getTest(ServerRequest request){
        return ok().contentType(MediaType.TEXT_PLAIN).body(Mono.just("ddd"),String.class);
    }

    public Mono<ServerResponse> getTime(ServerRequest request){
        return ok().contentType(MediaType.TEXT_EVENT_STREAM).body(Flux.interval(Duration.ofSeconds(1)).map(l->new SimpleDateFormat("HH:mm:ss").format(new Date())), String.class);
    }
}
